package com.msmall.community.admin.action;

import java.util.List;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantUrl;
import com.msmall.community.play.cases.model.Action;
import com.msmall.community.play.cases.model.Scenario;
import com.msmall.community.play.cases.model.Target;
import com.msmall.community.play.cases.service.ScenarioManager;

public class ScenarioAction extends HTMLBaseAction{

	private static final long serialVersionUID = 7671585524638825181L;

	private Long casesId;
	
	private Long scenarioId;
	
	private Long targetId;
	
	private Long actionId;
	
	private Scenario scenario;
	
	private Target target;
	
	private Action actionx;
	
	private List<Target> targets;
	
	private List<Action> actions;
	
	private ScenarioManager scenarioManager;

	/**
	 * 通过CASES ID进入场景首页
	 */
	public String execute(){
		page.setPageSize(20);
		scenarioManager.getScenariosByCases(page, casesId);
		page.setUrl(ConstantUrl.ADMIN_PLAY_CASES_SCENARIO_INDEX + "?casesId=" + casesId);
		return SUCCESS;
	}
	/**
	 * 创建新的场景SCENARIO,如果actionId, targetId为NULL/0表示开始场景
	 */
	public String create(){
		scenarioId = null;
		scenario = null;
		return SUCCESS;
	}
	/**
	 * 更新场景
	 */
	public String update(){
		scenario = (Scenario) scenarioManager.get(Scenario.class, scenarioId);
		return SUCCESS;
	}
	/**
	 * 删除场景
	 * @return
	 * @throws Exception 
	 */
	public String delete() throws Exception{
		scenarioManager.removeScenario(scenarioId);
		return SUCCESS;
	}
	/**
	 * 保存场景,如果actionId, targetId为NULL/0表示开始场景
	 */
	public String save(){
		scenarioManager.saveScenario(scenario, casesId, actionId, targetId);
		return SUCCESS;
	}
	/**
	 * 管理一个场景的动作/对象
	 * @throws Exception 
	 */
	public String manager() throws Exception{
		targets = scenarioManager.getTargetsByScenario(scenarioId);
		actions = scenarioManager.getActionsByScenario(scenarioId);
		scenario = (Scenario) scenarioManager.get(Scenario.class, scenarioId);
		return SUCCESS;
	}
	/**
	 * 创建动作
	 */
	public String createAction(){
		actionId = null;
		actionx = null;
		return SUCCESS;
	}
	/**
	 * 保存动作
	 */
	public String saveAction(){
		scenarioManager.saveAction(actionx, scenarioId);
		return SUCCESS;
	}
	/**
	 * 删除动作
	 * @throws Exception 
	 */
	public String deleteAction() throws Exception{
		scenarioManager.removeAction(actionId);
		return SUCCESS;
	}
	/**
	 * 创建动作对象
	 */
	public String createTarget(){
		targetId = null;
		target = null;
		return SUCCESS;
	}
	/**
	 * 保存动作对象
	 */
	public String saveTarget(){
		scenarioManager.saveTarget(target, scenarioId);
		return SUCCESS;
	}
	/**
	 * 删除动作对象
	 * @throws Exception 
	 */
	public String deleteTarget() throws Exception{
		scenarioManager.removeTarget(targetId);
		return SUCCESS;
	}
	
	public Long getCasesId() {
		return casesId;
	}

	public void setCasesId(Long casesId) {
		this.casesId = casesId;
	}

	public Long getScenarioId() {
		return scenarioId;
	}

	public void setScenarioId(Long scenarioId) {
		this.scenarioId = scenarioId;
	}

	public Long getTargetId() {
		return targetId;
	}

	public void setTargetId(Long targetId) {
		this.targetId = targetId;
	}

	public Long getActionId() {
		return actionId;
	}

	public void setActionId(Long actionId) {
		this.actionId = actionId;
	}

	public Scenario getScenario() {
		return scenario;
	}

	public void setScenario(Scenario scenario) {
		this.scenario = scenario;
	}

	public Target getTarget() {
		return target;
	}

	public void setTarget(Target target) {
		this.target = target;
	}


	public Action getActionx() {
		return actionx;
	}
	public void setActionx(Action actionx) {
		this.actionx = actionx;
	}
	public List<Target> getTargets() {
		return targets;
	}
	public void setTargets(List<Target> targets) {
		this.targets = targets;
	}
	public List<Action> getActions() {
		return actions;
	}
	public void setActions(List<Action> actions) {
		this.actions = actions;
	}
	public ScenarioManager getScenarioManager() {
		return scenarioManager;
	}

	public void setScenarioManager(ScenarioManager scenarioManager) {
		this.scenarioManager = scenarioManager;
	}
	
}
